package org.luxor.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luxor.commons.core.utils.BeanUtils;
import org.luxor.commons.core.utils.PassWordUtils;
import org.luxor.system.entity.SysUserEntity;
import org.luxor.system.mapper.SysUserMapper;
import org.luxor.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark @date 2020-07-26 16:52:23
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements ISysUserService {

    @Override
    public IPage<SysUserEntity> page(IPage<SysUserEntity> page, Map query) {
        SysUserEntity queryEntity = BeanUtils.toObject(SysUserEntity.class, query);
        return super.page(page, Wrappers.query(queryEntity));
    }

    @Override
    public boolean save(SysUserEntity sysUser) {
        sysUser.setPassword(PassWordUtils.encode(sysUser.getPassword()));
        return super.save(sysUser);
    }

    @Override
    public SysUserEntity getByUserName(String username) {
        Wrapper<SysUserEntity> queryWrapper = Wrappers.<SysUserEntity>lambdaQuery()
                .eq(SysUserEntity::getUserName, username);
        SysUserEntity sysUser = getBaseMapper().selectOne(queryWrapper);
        return sysUser;
    }

    @Override
    public Integer countByDeptId(Long deptId) {
        Wrapper<SysUserEntity> queryWrapper = Wrappers.<SysUserEntity>lambdaQuery()
                .eq(SysUserEntity::getDeptId, deptId);
        Integer deptUserCount = getBaseMapper().selectCount(queryWrapper);
        return deptUserCount;
    }

    @Override
    public List<SysUserEntity> listByDeptId(Long deptId) {
        Wrapper<SysUserEntity> queryWrapper = Wrappers.<SysUserEntity>lambdaQuery()
                .eq(SysUserEntity::getDeptId, deptId);
        List<SysUserEntity> deptUsers = getBaseMapper().selectList(queryWrapper);
        return deptUsers;
    }


}
